<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/12/12
 * Time: 19:20
 */

class FlowController extends AppController
{
    public $components = array('publicFunction','Excle');
    public $uses = ['Platform','Flow'];
    public $layout = null;

    public function prop_log()
    {
        $params = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        $where = array();
        $startTime = date('Y-m-d H:i:s',strtotime("-1 day"));
        $endTime = date('Y-m-d H:i:s');
        $prid = -1;
        unset($params["/flow/prop_log"]);
        if ($params) {
            $startTime = $params['starttime'] ?: '';
            $endTime = $params['endtime'] ?: '';
            $prid = $params['prid'] ?: -1;
            if ($prid != -1) {
                $where['Flow.prid'] = $prid;
            }
        }
        if ($startTime && $endTime) {
            $where['Flow.init_time BETWEEN ? and ?'] = array(strtotime($startTime), strtotime($endTime));
        }
        $res = array(
            'starttime' => $startTime,
            'endtime' => $endTime,
            'prid' => $prid,
        );
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 15;
        $count = $this->Flow->countAllPropLog($where);
        $pageCount = ceil($count / $limit);
        $prop = $this->Flow->findAllPropLog($where, $page, $limit);
        if ($_POST['export'] == "wyq") {
            $filename = "道具流水表";
            $data = [];
            foreach ($prop as $k => $v) {
                $data[$k]['uid'] = $v['Flow']['uid'];
                $data[$k]['username'] = $v['u']['username'];
                $data[$k]['prid'] = $v['Flow']['prid'];
                $data[$k]['prop_name'] = $v['p']['prop_name'];
                $data[$k]['num'] = $v['Flow']['num'];
                $data[$k]['status'] = $v['Flow']['status'];
                $data[$k]['init_time'] = date('Y-m-d h:i:s', $v['Flow']['init_time']);
            }
            $headArr = ['用户id','用户名','道具id','道具名', '道具描述', '数量', '时间'];
            $this->Excle->exportExcel($filename, $headArr, $data);
            exit;
        }
        $variable = $this->Platform->getAllProps([]);
        $props = [];
        foreach ($variable as $key => $val) {
            $props[$val['Platform']['id']] = $val['Platform']['prop_name'];
        }
        $url = '/flow/prop_log?starttime='.$startTime.'&endtime='.$endTime.'&prid='.$prid;
        $this->set(array('props' => $props, 'prid'=>$prid ,'prop' => $prop, 'res' => $res, 'page' => $page, 'pageCount' => $pageCount, 'url' => $url));
    }

    public function coin_log()
    {
        $params = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        $where = array();
        $startTime = date('Y-m-d H:i:s',strtotime("-1 day"));
        $endTime = date('Y-m-d H:i:s');
        if ($params['starttime']) {
            $startTime = $params['starttime'] ?: '';
            $endTime = $params['endtime'] ?: '';
        }
        if ($startTime && $endTime) {
            $where['Flow.init_time BETWEEN ? and ?'] = array(strtotime($startTime), strtotime($endTime));
        }
        $res = array(
            'starttime' => $startTime,
            'endtime' => $endTime,
        );
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 15;
        $count = $this->Flow->countAllCoinLog($where);
        $pageCount = ceil($count / $limit);
        if ($page > $pageCount){
            $page = 1;
        }
        $coin = $this->Flow->findAllCoinLog($where, $page, $limit);

        if ($_POST['export'] == "wyq") {
            $filename = "金币流水表";
            $data = [];
            foreach ($coin as $k => $v) {
                $data[$k]['uid'] = $v['Flow']['uid'];
                $data[$k]['username'] = $v['u']['username'];
                $data[$k]['amount'] = $v['Flow']['amount'];
                $data[$k]['desc'] = $v['Flow']['desc'];
                $data[$k]['init_time'] = date('Y-m-d h:i:s', $v['Flow']['init_time']);
            }
            $headArr = ['用戶id', '用户名', '金币数','描述' ,'记录时间'];
            $this->Excle->exportExcel($filename, $headArr, $data);
            exit;
        }
        $url = '/flow/coin_log?starttime='.$startTime.'&endtime='.$endTime;
        $this->set(array('coin' => $coin, 'res' => $res, 'page' => $page,'count' => $count, 'pageCount' => $pageCount, 'url' => $url));
    }
}